<?php
/**
 * 管理后台用户管理
 */

namespace app\auth\controller;


use app\auth\service\AdminUserService;
use app\auth\service\GroupService;
use app\common\controller\AdminBaseController;
use think\App;
use think\facade\View;

class AdminUser extends AdminBaseController
{
    protected $service;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new AdminUserService();
    }

    /**
     * 列表
     */
    public function index() {
        return View::fetch('index');
    }

    /**
     * 获取数据
     */
    public function getData() {
        $params = $this->request->get();
        $service = new AdminUserService();
        $data = $service->getData($params,$params['page'],$params['limit']);
        return ['code' => 0,'msg' => '获取数据成功','data' => $data['data'],'count' => $data['count']];
    }

    /**
     * 添加
     */
    public function add() {
        if($this->request->post()) {
            $params = $this->request->post();
            $service = new AdminUserService();
            $res = $service->addUser($params);
            return $res;
        }else {
            // 获取所有的角色
            $groupService = new GroupService();
            $group = $groupService->getAllGroup();
            return View::fetch('add',[
                'groups' => $group,
                'title' => '添加用户'
            ]);
        }
    }

    /**
     * 编辑
     */
    public function edit($id) {
        if($this->request->isPost()) {
            $params = $this->request->post();
            $service = new AdminUserService();
            return $service->editUser($params);
        }else {
            $detail = $this->service->detail($id);
            $groupService = new GroupService();
            $group = $groupService->getAllGroup();
            return View::fetch('edit', [
                'data' => $detail,
                'groups' => $group,
                'title' => '编辑用户'
            ]);
        }
    }

    /**
     * 删除
     */
    public function del($id) {
        return $this->service->del($id);
    }

    /**
     * 修改个人信息
     */
    public function persional() {
        if($this->request->isPost()) {
            $res = $this->service->persional($this->request->post());
            return $res;
        }else {
            $detail = session('admin');
            return view('persional',[
                'data' => $detail,
                'title' => '个人中心'
            ]);
        }
    }



}